package com.atguigu.spzx.manager.service.system.impl;

import com.atguigu.spzx.manager.helper.MenuHelper;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.service.system.SysMenuService;
import com.atguigu.spzx.model.entity.system.AuthContextUtil;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Mahone
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Override
    public List<SysMenu> findNodes() {
        // 查询到该表的所有数据
        List<SysMenu> sysMenuList = sysMenuMapper.selectAllNodes();
        return  MenuHelper.buildTree(sysMenuList);
    }

    @Override
    public void saveMenu(SysMenu sysMenu) {
        sysMenuMapper.insertSysMenu(sysMenu);
    }

    @Override
    public void updateSysMenuById(SysMenu sysMenu) {
        sysMenuMapper.updateSysMenuById(sysMenu);
    }

    @Override
    public void removeSysMenuById(Long id) {
        sysMenuMapper.deleteSysMenuById(id);
    }

    @Override
    public List<SysMenuVo> menus() {
        // 拦截器获取userId
        SysUser user = AuthContextUtil.getUser();
        // 查询当前登录用户所拥有的菜单
        List<SysMenu> sysMenuList = sysMenuMapper.selectMenuByUserId(user.getId());
        // 递归为树
        List<SysMenu> sysMenuListTree = MenuHelper.buildTree(sysMenuList);
        List<SysMenuVo> sysMenuVos = MenuHelper.buildVoTree(sysMenuListTree);
        return sysMenuVos;
    }


}
